package com.zs.test.eventbusdemo;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MainActivity extends AppCompatActivity {

    @InjectView(R.id.ll_main)
    LinearLayout mLayout;
    @InjectView(R.id.tv_main)
    TextView mTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        ButterKnife.inject(this);

    }

    public void turn(View view){
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        FragmentTest fragmentTest = new FragmentTest();
        transaction.add(R.id.ll_main,fragmentTest);
        transaction.commit();

    }

    /**
     * EventBus3.0之前消息处理的方法只能限定于
     * onEvent、onEventMainThread、onEventBackgroundThread和onEventAsync，
     * 他们分别代表四种线程模型。而在EventBus3.0之后，事件处理的方法可以随便取名，
     * 但是需要添加一个注解@Subscribe，并且要指定线程模型（默认为POSTING）
     * @param msg
     */

    @Subscribe(threadMode = ThreadMode.POSTING, priority = 100)
    public void getMsg(TestMsg msg){
        if (msg != null) {
            mTv.setText(msg.getName());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }
}
